import scrapy
import random
from scrapys.wudiSpyder.wudiSpyder.items import JingcaiTtgGameItem


class Wan500JingcaiTtgSpider(scrapy.Spider):
    name = "Wan500JingcaiTtgSpider"
    allowed_domains = ["500.com"]

    def start_requests(self):
        USER_AGENTS = [
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
            # 更多User-Agent...
        ]
        url = "https://trade.500.com/jczq"  # 500wan 足球总进球竞猜彩选购页面
        yield scrapy.Request(url=url, headers={'User-Agent': random.choice(USER_AGENTS)}, callback=self.parse)


    def parse(self, response):

        tables = response.xpath('//*[@id="relativeContainer"]/div/div[3]/table')
        for table in tables:
            trs = table.xpath("tbody/tr")
            for tr in trs:
                fid = tr.xpath("@data-fixtureid").get("data")  # 500wan比赛id
                expect = tr.xpath("@data-processdate").get("data")  # 竞猜比赛期数
                stop_time = tr.xpath("@data-buyendtime").get("data")  # 停止购买时间
                game_no_num = tr.xpath("@data-processname").get("data")  # 竞猜比赛场次
                game_no = tr.xpath("@data-matchnum").get("data")  # 竞猜比赛场次名称

                g0 = tr.xpath("td[6]/div[2]/p[@data-value='0']/@data-sp").get("data")    # 0球
                g1 = tr.xpath("td[6]/div[2]/p[@data-value='1']/@data-sp").get("data")  # 1球
                g2 = tr.xpath("td[6]/div[2]/p[@data-value='2']/@data-sp").get("data")  # 2球
                g3 = tr.xpath("td[6]/div[2]/p[@data-value='3']/@data-sp").get("data")  # 3球
                g4 = tr.xpath("td[6]/div[2]/p[@data-value='4']/@data-sp").get("data")  # 4球
                g5 = tr.xpath("td[6]/div[2]/p[@data-value='5']/@data-sp").get("data")  # 5球
                g6 = tr.xpath("td[6]/div[2]/p[@data-value='6']/@data-sp").get("data")  # 6球
                g7 = tr.xpath("td[6]/div[2]/p[@data-value='7']/@data-sp").get("data")  # 7球

                bonus = "{},{},{},{},{},{},{},{}".format(g0, g1, g2, g3, g4, g5, g6, g7)

                # 提取游戏信息
                game_item = JingcaiTtgGameItem()
                game_item['fid'] = fid
                game_item['expect'] = expect
                game_item['stop_time'] = stop_time
                game_item['game_no'] = game_no
                game_item['game_no_num'] = game_no_num
                game_item['ttg_bonus'] = bonus
                yield game_item



